package com.tadpole.music.view.fragment.question;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.core.content.ContextCompat;

import com.luck.picture.lib.permissions.RxPermissions;
import com.tadpole.music.R;
import com.tadpole.music.bean.question.RealQuestionBean;
import com.tadpole.music.config.Constant;
import com.tadpole.music.iView.question.RealQuestionIView;
import com.tadpole.music.presenter.question.RealQuestionPresenter;
import com.tadpole.music.utils.Mutils;
import com.tadpole.music.utils.NoDoubleClickListener;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;
import com.tadpole.music.view.activity.login.LoginActivity;
import com.tadpole.music.view.activity.me.MemberCenterActivity;
import com.tadpole.music.view.activity.question.EquipmentActivity;
import com.tadpole.music.view.activity.question.RealAnswerActivity;
import com.tadpole.music.view.activity.question.SpecialExercisesActivity;
import com.tadpole.music.view.fragment.base.BaseFragment;

import io.reactivex.functions.Consumer;

/**
 * 实战训练
 * Created By 孙岩
 * on 2020/9/11
 */
public class RealTrainFragment extends BaseFragment implements RealQuestionIView {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private static final String ARG_PARAM3 = "param3";
    //院校id
    private String mParam1;
    //等级id
    private String mParam2;
    private String mParam3;
    //开始考试
    private TextView tvExamination;
    //获取真题
    private RealQuestionPresenter realQuestionPresenter;

    private TextView tvOne;
    private TextView tvTwo;
    public static RealTrainFragment newInstance(String param1, String param2,String param3) {
        RealTrainFragment fragment = new RealTrainFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        args.putString(ARG_PARAM3, param3);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getContentViewId() {
        return R.layout.fragment_real_train;
    }

    @Override
    protected void initAllMembersView(Bundle savedInstanceState) {
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
            mParam3 = getArguments().getString(ARG_PARAM3);
        }
    }

    @Override
    protected void initView(View mRootView) {
        tvExamination = mRootView.findViewById(R.id.tvExamination);
        tvOne = mRootView.findViewById(R.id.tvOne);
        tvTwo = mRootView.findViewById(R.id.tvTwo);
        realQuestionPresenter = new RealQuestionPresenter();
        realQuestionPresenter.attachView(this);

        initListeners();
        setData();
    }

    private void setData(){
        SpannableString spannable = new SpannableString ("100%还原真实考试场景");
        //这个一定要记得设置，不然点击不生效
        tvOne.setMovementMethod(LinkMovementMethod.getInstance());
        tvOne.setHighlightColor(Color.TRANSPARENT);
        //设置文字的前景色
        spannable.setSpan(getRedSpan(),0,4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvOne.setText(spannable);

        if(mParam1.equals("1")){

            if(mParam2.equals("1")){//初级
                SpannableString spannable1 = new SpannableString ("试卷分数：100分。乐理40分，音乐常识40分，视唱模唱20分答题总量：45题 答题时间：60分钟");
                //这个一定要记得设置，不然点击不生效
                tvTwo.setMovementMethod(LinkMovementMethod.getInstance());
                tvTwo.setHighlightColor(Color.TRANSPARENT);
                //设置文字的前景色
                spannable1.setSpan(getYellowSpan(),5,8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannable1.setSpan(getYellowSpan(),12,14, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannable1.setSpan(getYellowSpan(),20,22, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannable1.setSpan(getYellowSpan(),28,30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannable1.setSpan(getYellowSpan(),36,38, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannable1.setSpan(getYellowSpan(),45,47, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                tvTwo.setText(spannable1);
            }else{
                SpannableString spannable1 = new SpannableString ("试卷分数：100分。乐理44分，音乐常识36分，视唱模唱20分答题总量：43题 答题时间：60分钟");
                //这个一定要记得设置，不然点击不生效
                tvTwo.setMovementMethod(LinkMovementMethod.getInstance());
                tvTwo.setHighlightColor(Color.TRANSPARENT);
                //设置文字的前景色
                spannable1.setSpan(getYellowSpan(),5,8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannable1.setSpan(getYellowSpan(),12,14, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannable1.setSpan(getYellowSpan(),20,22, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannable1.setSpan(getYellowSpan(),28,30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannable1.setSpan(getYellowSpan(),36,38, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannable1.setSpan(getYellowSpan(),45,47, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                tvTwo.setText(spannable1);
            }

        }else if(mParam1.equals("2")){
            SpannableString spannable1 = new SpannableString ("试卷分数：100分。每题各 4分。答题总量：25题。");
            //这个一定要记得设置，不然点击不生效
            tvTwo.setMovementMethod(LinkMovementMethod.getInstance());
            tvTwo.setHighlightColor(Color.TRANSPARENT);
            //设置文字的前景色
            spannable1.setSpan(getYellowSpan(),5,8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannable1.setSpan(getYellowSpan(),13,15, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannable1.setSpan(getYellowSpan(),22,24, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            tvTwo.setText(spannable1);
        }else if(mParam1.equals("3")){
            SpannableString spannable1 = new SpannableString ("试卷分数：100分。选择题 40道，每题各 2分。视唱模唱各 10分。答题总量：4题 答题时间：60分钟");
            //这个一定要记得设置，不然点击不生效
            tvTwo.setMovementMethod(LinkMovementMethod.getInstance());
            tvTwo.setHighlightColor(Color.TRANSPARENT);
            //设置文字的前景色
            spannable1.setSpan(getYellowSpan(),5,8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannable1.setSpan(getYellowSpan(),13,16, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannable1.setSpan(getYellowSpan(),21,23, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannable1.setSpan(getYellowSpan(),30,33, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannable1.setSpan(getYellowSpan(),40,42, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannable1.setSpan(getYellowSpan(),49,51, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            tvTwo.setText(spannable1);
        }else if(mParam1.equals("6")){
            SpannableString spannable1 = new SpannableString ("试卷分数：200分。乐理题 100分。 听辨题 100分");
            //这个一定要记得设置，不然点击不生效
            tvTwo.setMovementMethod(LinkMovementMethod.getInstance());
            tvTwo.setHighlightColor(Color.TRANSPARENT);
            //设置文字的前景色
            spannable1.setSpan(getYellowSpan(),5,8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannable1.setSpan(getYellowSpan(),13,17, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannable1.setSpan(getYellowSpan(),23,27, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            tvTwo.setText(spannable1);
        } else{
            SpannableString spannable1 = new SpannableString ("试卷分数：100分。每题各 4分。答题总量：25题。");
            //这个一定要记得设置，不然点击不生效
            tvTwo.setMovementMethod(LinkMovementMethod.getInstance());
            tvTwo.setHighlightColor(Color.TRANSPARENT);
            //设置文字的前景色
            spannable1.setSpan(getYellowSpan(),5,8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannable1.setSpan(getYellowSpan(),13,15, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannable1.setSpan(getYellowSpan(),22,24, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            tvTwo.setText(spannable1);
        }
    }


    private ForegroundColorSpan getRedSpan(){
        return new ForegroundColorSpan(ContextCompat.getColor(getActivity(),R.color.color_red));
    }

    private ForegroundColorSpan getYellowSpan(){
        return new ForegroundColorSpan(ContextCompat.getColor(getActivity(),R.color.color_yellow));
    }


    private void initListeners(){
        tvExamination.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if(Mutils.isNetworkAvailable()){
                        andPermission();
                }else{
                    ToastUtils.show("请检查您的网络设置");
                }
            }
        });
    }

    @Override
    public void showRealQuestion(RealQuestionBean.DataBeanX bean) {

        //调整设备
        Intent intent = new Intent(getActivity(), EquipmentActivity.class);
        intent.putExtra("college_id",mParam1);
        intent.putExtra("rank_id",mParam2);
        startActivity(intent);

    }

    @Override
    public void show401() {
        Intent intent = new Intent(getActivity(), LoginActivity.class);
        startActivity(intent);
        SpUtil.getInstance(getActivity()).putString(Constant.TOKEN,"");
    }

    @Override
    public void showGoPay() {
        Intent intent = new Intent(getActivity(), MemberCenterActivity.class);
        intent.putExtra("flag","1");
        intent.putExtra("college_id",mParam1);
        intent.putExtra("rank_id",mParam2);
        startActivity(intent);
    }

    //权限
    @SuppressLint("CheckResult")
    private void andPermission() {
        RxPermissions rxPermission = new RxPermissions(getActivity());
        rxPermission
                .request(
                        Manifest.permission.MODIFY_AUDIO_SETTINGS,
                        Manifest.permission.RECORD_AUDIO,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE
                ).subscribe(new Consumer<Boolean>() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                //请求权限成功
                if (aBoolean) {
                    realQuestionPresenter.getRealQuestion(mParam1,mParam2,1);
                } else {//失败
                    String msg = "请点击【去授权】打开相应权限！";
                    showAlert(msg);
                }
            }
        });
    }

    //授权
    public void showAlert(String msg) {
        new AlertDialog.Builder(getActivity()).setMessage(msg).
                setPositiveButton("去授权",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                                Uri uri = Uri.fromParts("package", getActivity().getPackageName(), null);
                                intent.setData(uri);
                                startActivity(intent);
                            }
                        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).setCancelable(true).show();
    }
}
